package app.itgungnir.compose.wan.business.coin_rank

import android.content.res.Configuration
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import app.itgungnir.compose.wan.utils.LocalDataStore
import app.itgungnir.compose.wan.widgets.WanHeader
import app.itgungnir.compose.wan.widgets.WanTagButton
import app.itgungnir.compose.wan.widgets.WanTitle

@Composable
fun CoinRankItem(modifier: Modifier = Modifier, item: CoinRank?) {
    item ?: return
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        val isMe = item.userId == LocalDataStore.localData.userInfo.id
        WanHeader(content = item.rank)
        Spacer(modifier = Modifier.width(10.dp))
        WanTagButton(text = "Lv.${item.level}", color = MaterialTheme.colors.secondary)
        Spacer(modifier = Modifier.width(5.dp))
        if (isMe) {
            WanTagButton(text = "ME", color = MaterialTheme.colors.primary)
            Spacer(modifier = Modifier.width(5.dp))
        } else {
            Spacer(modifier = Modifier.width(5.dp))
        }
        WanTitle(
            modifier = Modifier.weight(1F),
            content = when (isMe) {
                true -> LocalDataStore.localData.userInfo.name
                else -> item.username ?: "****"
            },
            maxLines = 1
        )
        Spacer(modifier = Modifier.width(10.dp))
        WanHeader(content = "${item.coinCount}", color = MaterialTheme.colors.secondary)
    }
}

@Preview(name = "CoinRankItem light mode", showBackground = true)
@Preview(name = "CoinRankItem dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun CoinRankItemPreview() {
    WanComposePreviewTheme {
        CoinRankItem(
            item = CoinRank(
                rank = "102",
                level = 43,
                username = "IT**ir 888888888888888888",
                coinCount = 4469L
            )
        )
    }
}
